import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    menus: '' // 菜单
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  // 新增
  SET_MENUS: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { name, pwd } = userInfo
    return new Promise((resolve, reject) => {
      login({
        name: name.trim(),
        pwd: pwd
      })
        .then((response) => {
          const { data } = response
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          const { data } = response

          if (!data) {
            return reject('验证失败,请重新登录.')
          }

          const { trueName, userImg } = data
          const menus = []
          // 请求数据
          if (data.roleId === 1) {
            menus.push(
              {
                path: '/sys',
                component: 'Layout',
                redirect: '/sys/user',
                meta: {
                  title: '系统管理',
                  icon: 'el-icon-setting'
                },
                children: [
                  {
                    path: 'user',
                    name: 'user',
                    component: 'sys/user/index',
                    meta: {
                      title: '用户管理',
                      icon: 'user'
                    }
                  },
                  {
                    path: 'log/login',
                    component: 'sys/log/loginLog',
                    meta: {
                      title: '登录日志',
                      icon: 'el-icon-info'
                    }
                  },
                  {
                    path: 'log/operate',
                    component: 'sys/log/operateLog',
                    meta: {
                      title: '操作日志',
                      icon: 'table'
                    }
                  }
                ]
              },

              {
                path: '/lab',
                component: 'Layout',
                meta: {
                  title: '实验室管理',
                  icon: 'el-icon-s-tools'
                },
                children: [
                  {
                    path: '/lab',
                    component: 'lab/lab/index',
                    meta: {
                      title: '实验室管理',
                      icon: 'el-icon-office-building'
                    }
                  },
                  {
                    path: '/labSub',
                    component: 'lab/labSub/index',
                    meta: {
                      title: '分室管理',
                      icon: 'el-icon-school'
                    }
                  },
                  {
                    path: '/lab-booking',
                    component: 'lab/lab-booking/index',
                    meta: {
                      title: '预约管理',
                      icon: 'el-icon-school'
                    }
                  }
                ]
              },

              {
                path: '/course',
                component: 'Layout',
                redirect: '/course/index',
                meta: {
                  title: '课程管理',
                  icon: 'form'
                },
                children: [
                  {
                    path: 'index',
                    component: 'course/index',
                    meta: {
                      title: '课程信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'course/edit',
                    meta: {
                      title: '课程信息添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'course/edit',
                    meta: {
                      title: '课程信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },

              {
                path: '/device',
                component: 'Layout',
                redirect: '/device/index',
                meta: {
                  title: '设备管理',
                  icon: 'el-icon-s-platform'
                },
                children: [
                  {
                    path: 'index',
                    component: 'device/device/index',
                    meta: {
                      title: '设备管理',
                      icon: 'el-icon-monitor'
                    }
                  },
                  {
                    path: 'repair',
                    component: 'device/repair/index',
                    meta: {
                      title: '报修管理',
                      icon: 'el-icon-document-delete'
                    }
                  }
                ]
              },

              {
                path: '/person',
                component: 'Layout',
                meta: {
                  title: '人员管理',
                  icon: 'el-icon-s-custom'
                },
                redirect: '/person/index',
                children: [
                  {
                    path: 'index',
                    component: 'person/index',
                    meta: {
                      title: '人员信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'person/edit',
                    meta: {
                      title: '人员信息添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'person/edit',
                    meta: {
                      title: '人员信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },

              {
                path: '/project',
                component: 'Layout',
                redirect: '/project/index',
                meta: {
                  title: '课程项目管理',
                  icon: 'el-icon-document'
                },
                children: [
                  {
                    path: 'index',
                    component: 'project/index',
                    meta: {
                      title: '课程项目列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'project/edit',
                    meta: {
                      title: '课程项目添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'project/edit',
                    meta: {
                      title: '课程项目修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },

              {
                path: '/open',
                component: 'Layout',
                redirect: '/open/index',
                meta: {
                  title: '实验室开放管理',
                  icon: 'el-icon-full-screen'
                },
                children: [
                  {
                    path: 'index',
                    component: 'open/index',
                    meta: {
                      title: '开放信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'open/edit',
                    meta: {
                      title: '开放信息添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'open/edit',
                    meta: {
                      title: '开放信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              }
            )
          } else if (data.roleId === 2) {
            menus.filter((item) => item > 1)
            menus.push(
              {
                path: '/lab',
                component: 'Layout',
                meta: {
                  title: '实验室管理',
                  icon: 'el-icon-s-tools'
                },
                children: [
                  {
                    path: '/lab',
                    component: 'lab/lab/index',
                    meta: {
                      title: '实验室管理',
                      icon: 'el-icon-office-building'
                    }
                  },
                  {
                    path: '/labSub',
                    component: 'lab/labSub/index',
                    meta: {
                      title: '分室管理',
                      icon: 'el-icon-school'
                    }
                  },
                  {
                    path: '/lab-booking',
                    component: 'lab/lab-booking/index',
                    meta: {
                      title: '预约管理',
                      icon: 'el-icon-school'
                    }
                  }
                ]
              },

              {
                path: '/course',
                component: 'Layout',
                redirect: '/course/index',
                meta: {
                  title: '课程管理',
                  icon: 'form'
                },
                children: [
                  {
                    path: 'index',
                    component: 'course/index',
                    meta: {
                      title: '课程信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'course/edit',
                    meta: {
                      title: '课程信息添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'course/edit',
                    meta: {
                      title: '课程信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },

              {
                path: '/person',
                component: 'Layout',
                redirect: '/person/index',
                meta: {
                  title: '人员管理',
                  icon: 'el-icon-s-custom'
                },
                children: [
                  {
                    path: 'index',
                    component: 'person/index',
                    meta: {
                      title: '人员信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'person/edit',
                    meta: {
                      title: '人员信息添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'person/edit',
                    meta: {
                      title: '人员信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },

              {
                path: '/project',
                component: 'Layout',
                redirect: '/project/index',
                meta: {
                  title: '课程项目管理',
                  icon: 'el-icon-document'
                },
                children: [
                  {
                    path: 'index',
                    component: 'project/index',
                    meta: {
                      title: '课程项目列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'add',
                    component: 'project/edit',
                    meta: {
                      title: '课程项目添加',
                      icon: 'el-icon-plus'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: 'project/edit',
                    meta: {
                      title: '课程项目修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },

              {
                path: '/open',
                component: 'Layout',
                redirect: '/open/index',
                meta: {
                  title: '实验室开放管理',
                  icon: 'el-icon-full-screen'
                },
                children: [
                  {
                    path: 'index',
                    component: 'open/index',
                    meta: {
                      title: '开放信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: '403',
                    meta: {
                      title: '开放信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              }
            )
          } else if (data.roleId === 3) {
            menus.filter((item) => item > 1)
            menus.push(
              {
                path: '/lab',
                component: 'Layout',
                meta: {
                  title: '实验室管理',
                  icon: 'el-icon-s-tools'
                },
                children: [
                  {
                    path: '/lab',
                    component: 'lab/lab/index',
                    meta: {
                      title: '实验室管理',
                      icon: 'el-icon-office-building'
                    }
                  },
                  {
                    path: '/labSub',
                    component: 'lab/labSub/index',
                    meta: {
                      title: '分室管理',
                      icon: 'el-icon-school'
                    }
                  },
                  {
                    path: '/lab-booking',
                    component: 'lab/lab-booking/index',
                    meta: {
                      title: '预约管理',
                      icon: 'el-icon-school'
                    }
                  }
                ]
              },
              {
                path: '/course',
                component: 'Layout',
                meta: {
                  title: '课程管理',
                  icon: 'form'
                },
                children: [
                  {
                    path: 'index',
                    component: 'course/index',
                    meta: {
                      title: '课程信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: '403',
                    meta: {
                      title: '课程信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },
              {
                path: '/project',
                component: 'Layout',
                redirect: '/project/index',
                meta: {
                  title: '课程项目管理',
                  icon: 'el-icon-document'
                },
                children: [
                  {
                    path: 'index',
                    component: 'project/index',
                    meta: {
                      title: '课程项目列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: '403',
                    meta: {
                      title: '课程项目修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              },
              {
                path: '/open',
                component: 'Layout',
                redirect: '/open/index',
                meta: {
                  title: '实验室开放管理',
                  icon: 'el-icon-full-screen'
                },
                children: [
                  {
                    path: 'index',
                    component: 'open/index',
                    meta: {
                      title: '开放信息列表',
                      icon: 'table'
                    }
                  },
                  {
                    path: 'edit/:id',
                    component: '403',
                    meta: {
                      title: '开放信息修改',
                      icon: 'el-icon-refresh'
                    },
                    hidden: true
                  }
                ]
              }
            )
          } else {
            menus.filter((item) => item > 1)
          }

          menus.push(
            {
              path: '/404',
              component: '404',
              hidden: true
            },
            {
              path: '*',
              redirect: '/404',
              hidden: true
            }
          )

          // 角色id
          sessionStorage.setItem('roleId', data.roleId)
          // 用户id
          sessionStorage.setItem('userId', data.id)
          commit('SET_NAME', trueName)
          commit('SET_AVATAR', userImg)
          commit('SET_MENUS', menus)
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('RESET_STATE')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
